package whf.framework.validate;

import whf.framework.exception.AppException;
import whf.framework.i18n.ApplicationResource;

/**
 * @author wanghaifeng
 *
 */
public class ValidateException extends AppException {
	private final static String VALIDATE_ERROR = "validate.error";
	
	private final static String VALIDATE_ERROR_PROPERTY = "validate.error.property";
	
	private final static String VALIDATE_SUCCESS = "validate.success";
	
	private ValidateErrors errors;
	
	public ValidateException(ValidateErrors errors) {
		this.errors = errors;
	}

	public ValidateErrors getErrors(){
		return this.errors;
	}
	
	public String toString(){
		if(this.errors == null || this.errors.isEmpty()){
			return ApplicationResource.get(VALIDATE_SUCCESS);
		}
		StringBuilder sb = new StringBuilder();
		sb.append(ApplicationResource.get(VALIDATE_ERROR));
		for(String propertyName: this.errors.getErrors().keySet()){
			sb.append(ApplicationResource.get(VALIDATE_ERROR_PROPERTY,new String[]{propertyName, this.errors.get(propertyName)}));
		}
		return sb.toString();
	}
}
